package com.baizhi.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class MybatisUtil {
	private static SqlSessionFactory factory;
	static {
		Reader in = null;
		try {
			in = Resources.getResourceAsReader("mybatis-config.xml");
			factory = new SqlSessionFactoryBuilder().build(in);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	public static SqlSession openSession() {
		SqlSession sqlsession = factory.openSession();
		return sqlsession;
	}
	public static void close(SqlSession sqlSession) {
		sqlSession.close();
	}
}
